package yanyi.community.controller;

import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import yanyi.community.enums.SortEnum;
import yanyi.community.provider.HotTagCache;
import yanyi.community.service.QuestionService;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 首页
 * @author yanyi
 */
@Controller
public class IndexController {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private HotTagCache hotTagCache;

    @RequestMapping("/")
    public String index(@RequestParam(value = "pageNum", defaultValue = "1", required = false) int pageNum,
                        @RequestParam(value = "pageSize", defaultValue = "3", required = false) int pageSize,
                        @RequestParam(value = "search", required = false) String search,
                        @RequestParam(value = "tag", required = false) String tag,
                        @RequestParam(value = "sort", required = false) String sort,
                        Model model) {
        HashMap<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(search)) {
            String[] tags = StringUtils.split(search, " ");
            search = Arrays
                    .stream(tags)
                    .filter(StringUtils::isNotBlank)
                    .map(t -> t.replace("+", "").replace("*", "").replace("?", ""))
                    .filter(StringUtils::isNotBlank)
                    .collect(Collectors.joining("|"));
            map.put("search", search);
        }
        if (StringUtils.isNotBlank(tag)) {
            tag = tag.replace("+", "").replace("*", "").replace("?", "");
            map.put("tag", tag);
        }
        if (StringUtils.isNotBlank(sort)) {
            Long time = 0L;
            for (SortEnum sortEnum : SortEnum.values()) {
                if (sortEnum.name().toLowerCase().equals(sort)) {
                    map.put("sort", sort);
                    if (sortEnum == SortEnum.HOT7) {
                        time = System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 7;
                    }
                    if (sortEnum == SortEnum.HOT30) {
                        time = System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30;
                    }
                    map.put("time", time);
                    break;
                }
            }
        }
        List<String> tags = hotTagCache.getHots();
        PageInfo questionList = questionService.findAll(map, pageNum, pageSize);
        model.addAttribute("questions", questionList);
        model.addAttribute("search", search);
        model.addAttribute("tag", tag);
        model.addAttribute("tags", tags);
        if (StringUtils.isBlank(sort)) {
            model.addAttribute("sort", "");
        } else {
            model.addAttribute("sort", sort);
        }
        return "index";
    }
}
